function getWeek(date) {
  let week
  let weekNum = date.getDay()
  switch (weekNum) {
    case 0:
      week = '周日'
      break
    case 1:
      week = '周一'
      break
    case 2:
      week = '周二'
      break
    case 3:
      week = '周三'
      break
    case 4:
      week = '周四'
      break
    case 5:
      week = '周五'
      break
    case 6:
      week = '周六'
      break
  }
  return week
}
export function getWeekByString(dateStr) {
  let val = new Date(dateStr || null)
  return getWeek(val)
}

export const getDateObj = time => {
  if (time.toString().length == 10) {
    time = time * 1000
  }
  time = Number(time)
  time = time || Date.now()
  let date = new Date(time)
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  return {
    year: Y,
    month: M,
    day: D,
    week: getWeek(date)
  }
}

// 3月4日周一
export const getDate = time => {
  let dateObj = getDateObj(time || Date.now())
  return dateObj.month + '月' + dateObj.day + '日' + dateObj.week
}

// 3月4日
export const getSimpleDate = time => {
  let dateObj = getDateObj(time || Date.now())
  return dateObj.month + '月' + dateObj.day + '日'
}

// 2019-03-04
export const dateFormat = time => {
  let dateObj = getDateObj(time || Date.now())
  let month = dateObj.month < 10 ? '0' + dateObj.month : dateObj.month
  let day = dateObj.day < 10 ? '0' + dateObj.day : dateObj.day
  return dateObj.year + '-' + month + '-' + day
}

// 相差天数
export const getRemainderTime = (time1, time2) => {
  time1 = Number(time1)
  time2 = Number(time2)
  let runTime = parseInt((time2 - time1) / 1000)
  return Math.floor(runTime / 86400)
}

// 2019-08-16 18:12:51
export const timeFormat = (date) => {
  function clear(str) {
    if (str.charAt(0) === '0') {
      return str.charAt(1)
    }
    return Number(str)
  }

  let arr = date.split(' ')
  let dateArr = arr[0].split('-')
  let timeArr = arr[1].split(':')
  let year = Number(dateArr[0])
  let month = clear(dateArr[1]) - 1
  let day = clear(dateArr[2])

  let hour = clear(timeArr[0])
  let min = clear(timeArr[1])
  let sec = clear(timeArr[2])
  return new Date(year, month, day, hour, min, sec).getTime()
}

// 填写订单页 日期返回
export const formatTime = (time, format) => {
  let t = format.replace('yyyy',time.year).replace('MM',time.month).replace('dd',time.day).replace('hh',time.hour).replace('mm',time.minute)
  return t
}
